//
//  ScrollCollectionReusableView.m
//  WuShiCity
//
//  Created by chengjie on 16/7/7.
//  Copyright © 2016年 chengjie. All rights reserved.
//

#import "ScrollCollectionReusableView.h"

@interface ScrollCollectionReusableView  ()<UIScrollViewDelegate>

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, assign)NSInteger arrayCount;
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, strong)UIPageControl *pageControl;


@end

@implementation ScrollCollectionReusableView

- (void)initWithFrame:(CGRect)frame imagesArray:(NSArray *)imagesArr period:(CGFloat)period{


    self.arrayCount = imagesArr.count;
    [self layoutSubviewsWithImagesArray:imagesArr];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    self.scrollView.delegate = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:period target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [self.timer setFireDate:[NSDate distantPast]];
}


- (void)layoutSubviewsWithImagesArray:(NSArray *)imagesArr {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width*(imagesArr.count+2), self.frame.size.height);
    for (int i = 0; i < imagesArr.count+1; i++) {
        
        UIImage *image = [[UIImage alloc] init];
        if (i == 0) {
            
            image = [imagesArr lastObject];
        }else if (i == imagesArr.count+1) {
            
            image = [imagesArr firstObject];
        }else {
            
            image = [imagesArr objectAtIndex:i-1];
        }
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width*i, 0, self.frame.size.width, self.frame.size.height)];
        imageView.image = image;
        [self.scrollView addSubview:imageView];
    }
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height-20, self.frame.size.width, 20)];
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = imagesArr.count;
    [self addSubview:self.pageControl];
    self.pageControl.tintColor = [UIColor brownColor];
    self.pageControl.pageIndicatorTintColor = [UIColor brownColor];
    
    [self.pageControl addTarget:self action:@selector(selectImage) forControlEvents:UIControlEventValueChanged];
}

- (void)nextImage {
    
    CGPoint point = self.scrollView.contentOffset;
    point.x = (point.x/self.frame.size.width+1)*self.frame.size.width;
    [self.scrollView setContentOffset:point animated:YES];
    self.pageControl.currentPage = point.x/self.frame.size.width-1;
}

- (void)selectImage {
    
    CGPoint point = self.scrollView.contentOffset;
    point.x = point.x * (self.pageControl.currentPage + 1);
    [self.scrollView setContentOffset:point animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger sumPage = scrollView.contentSize.width/self.frame.size.width;
    if (scrollView.contentOffset.x == 0) {
        
        [scrollView setContentOffset:CGPointMake(self.frame.size.width*(sumPage-2), 0) animated:NO];
        self.pageControl.currentPage = sumPage-3;
    }
    if (scrollView.contentOffset.x == (sumPage-1)*self.frame.size.width) {
        
        [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        self.pageControl.currentPage = 0;
    }
    //    self.pageControl.currentPage = sumPage;
}



@end
